<template>
<view class="page publish div">
	<form @submit="onFormSubmit" @reset="onFormReset">
  <view class="form">
    <view class="weui-cells div">
      <view class="weui-cell div">
        <view class="weui-cell__bd div">
          <input name="title" class="weui-input theme input" type="text" placeholder="活动主题" :value="activity.title"></input>
        </view>
      </view>
      <view class="weui-cell div">
        <view class="weui-cell__bd">
          <textarea name="description" class="weui-textarea textarea" placeholder="活动详情" :value="activity.description" rows="5"></textarea>
        </view>
      </view>
      <view class="weui-cell div">
        <view class="weui-cell__hd div">
          <view class="weui-label label">
            <text>报名截止时间 </text>
          </view>
        </view>
        <picker name="joindeadline" mode="date" :value="joinDeadline.selected" :start="joinDeadline.ranage.start" :end="joinDeadline.ranage.end" @change="onJoinDeadlineDateChange">
              <view class="picker">
              {{joinDeadline.selected}}
              </view>
        </picker>
      </view>
       <view class="weui-cell div">
              <view class="weui-cell__hd"><label for class="weui-label">每人收取费用<text class="text-muted">(线下收取)</text></label></view>
              <view class="weui-cell__bd">
                  <input class="weui-input" type="number" pattern value></input>
              </view>
              <view class="weui-cell__ft">
                  元
              </view>
          </view>
    </view>
    <view class="info-must">
      <view>选择报名者必填信息</view>
      <checkbox-group @change="checkboxChange">
        <label v-for="(item, index) in items" :key="index" class="checkbox">
          <checkbox :value="item.name" :checked="item.checked"></checkbox>{{item.value}}
        </label>
      </checkbox-group>
    </view>
    <view class="weui-btn-area div">
      <button class="weui-btn weui-btn_primary btn" style="color:#fff" formType="submit">发布</button>
    </view>
  </view>
  </form>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
      joinDeadline: {
        selected: '2017-06-05',
        ranage: {
          start: '2016-01-01',
          end: '2020-01-01'
        }
      },
      ranage: {
        start: '2016-01-01',
        end: '2020-01-01'
      },
      session: {},
      activity: {
        id: 0
      },
      position: {
        longitude: 0,
        latitude: 0
      },
      items: [{
        name: 'username',
        value: '填写真实姓名'
      }, {
        name: 'tel',
        value: '填写手机号码'
      }]
    };
  },

  components: {},
  props: {},
  onLoad: function (request) {
    var $this = this;
    $this.setData({
      'joinDeadline.ranage': {
        start: new app.globalData.util.date().dateAdd('y', -1)
      },
      'joinDeadline.selected': new app.globalData.util.date().dateToStr('yyyy-MM-dd', new app.globalData.util.date().dateAdd('w', 2))
    });

    if (request.id) {
      app.globalData.util.request({
        url: 'entry/wxapp/detail',
        data: {
          m: 'rcdonkey_signup',
          id: request.id
        },
        method: 'post',
        success: function (response) {
          $this.setData({
            'activity': response.data.data
          });
          $this.setData({
            'joinDeadline.selected': response.data.data.join_deadline_date
          });
        }
      });
    }
  },
  onReady: function () {
    var $this = this;
    app.globalData.util.getUserInfo(function (response) {
      $this.setData({
        session: response
      });
    });
    wx.getLocation({
      type: 'gcj02',
      success: function (position) {
        $this.setData({
          position: {
            longitude: position.longitude,
            latitude: position.latitude
          }
        });
      }
    });
  },
  methods: {
    onJoinDeadlineDateChange: function (event) {
      this.setData({
        'joinDeadline.selected': event.detail.value
      });
    },
    onFormSubmit: function (event) {
      var $this = this;
      var form = event.detail.value;

      if (!form.title) {
        wx.showToast({
          title: '请输入活动主题',
          icon: 'error',
          duration: 2000
        });
      }

      if (!form.description) {
        wx.showToast({
          title: '请输入活动详情',
          icon: 'error',
          duration: 2000
        });
      }

      app.globalData.util.request({
        url: 'entry/wxapp/postactivity',
        data: {
          m: 'rcdonkey_signup',
          title: form.title,
          description: app.globalData.util.parseContent(form.description),
          //处理内容中的特殊字符，比如emoji
          joindeadline: form.joindeadline,
          id: $this.data.activity.id,
          longitude: $this.data.position.longitude,
          latitude: $this.data.position.latitude
        },
        method: 'post',
        success: function (response) {
          wx.showToast({
            title: response.data.message,
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.redirectTo({
                url: '/rcdonkey_signup/pages/detail/detail?id=' + response.data.data.id
              });
            }
          });
        },
        fail: function (response) {
          if (response.data.message) {
            app.globalData.util.message(response.data.message, '', 'error');
          }
        }
      });
    }
  }
};
</script>
<style>
@import "./publish.css";
</style>